package com.yanqu.road.server.logic.task.condition;

import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.player.UserBeauty;
import com.yanqu.road.entity.task.TaskConditionInfo;
import com.yanqu.road.entity.task.args.BeautyArgs;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.task.TaskModule;
import com.yanqu.road.server.logic.task.BaseCondition;
import com.yanqu.road.server.logic.task.BaseTaskData;
import com.yanqu.road.server.manger.config.BeautyMgr;

import java.math.BigInteger;

/**
 * 美女累计传唤次数
 */
public class BeautyTotalPromotionTimesCondition extends BaseCondition {

	public BeautyTotalPromotionTimesCondition(TaskConditionInfo conditionInfo, BaseTaskData quest, BigInteger value, Object initArgs) {
		super(conditionInfo, quest, value, initArgs);
	}

	@Override
	public void addTrigger(GamePlayer player) {
		player.getModule(TaskModule.class).getGameEvent().addListener(eGamePlayerEventType.BeautyTotalPromotionTimes.getValue(), handle);
	}

	@Override
	public void removeTrigger(GamePlayer player) {
		player.getModule(TaskModule.class).getGameEvent().removeListener(eGamePlayerEventType.BeautyTotalPromotionTimes.getValue(), handle);
	}

	public boolean isCompleted(GamePlayer player) {
		BigInteger param4 = taskConditionInfo.getParamList().get(3);
		return getValue().compareTo(param4) >= 0;
	}

	@Override
	protected void changeValue(Object object) {
		BeautyArgs args = (BeautyArgs) object;
		long totalTimes = 0;
		if(null != args && null != args.getBeautyMap()) {
			for(UserBeauty userBeauty : args.getBeautyMap().values()){
				if(BeautyMgr.isUserBeautyExist(userBeauty)){
					int times = userBeauty.getIdentityLv() - 1;
					if(times < 0){
						times = 0;
					}
					totalTimes += times;
				}
			}
		}
		setValue(BigInteger.valueOf(totalTimes));
	}

}
